package com.zh.kotlin_mvvm.utils

import android.util.Log
import com.liulishuo.filedownloader.BaseDownloadTask
import com.liulishuo.filedownloader.FileDownloadListener
import com.liulishuo.filedownloader.FileDownloader
import com.zh.common.update.DownloadListener

/**
 * 提供一个创建下载任务的方法
 * @param url 下载链接
 * @param savePath 保存路径
 * @param call 下载监听,不要做耗时操作
 */
private fun createDownloadTask(
    url: String, savePath: String, call: DownloadListener? = null
) {
    val downloadTask = FileDownloader.getImpl()
        .create(url)
        .setPath(savePath, true)
        .setListener(listener(call))

    if (downloadTask.isUsing) {
        downloadTask.reuse()
    }
    downloadTask.start()
}

private fun listener(call: DownloadListener? = null): FileDownloadListener {
    return object : FileDownloadListener() {
        //等待，已经进入下载队列
        override fun pending(task: BaseDownloadTask, soFarBytes: Int, totalBytes: Int) {
            Log.i(
                "FileDownloader",
                "pending: soFarBytes = $soFarBytes totalBytes = $totalBytes"
            )
            call?.onStart()
        }

        //结束了pending，并且开始当前任务的Runnable
        override fun started(task: BaseDownloadTask?) {
            super.started(task)
            Log.i("FileDownloader", "started")
        }

        //已经连接上
        override fun connected(
            task: BaseDownloadTask,
            etag: String,
            isContinue: Boolean,
            soFarBytes: Int,
            totalBytes: Int
        ) {
            Log.i(
                "FileDownloader",
                "connected: etag = $etag isContinue = $isContinue soFarBytes = $soFarBytes totalBytes = $totalBytes"
            )
        }

        //下载进度回调
        override fun progress(task: BaseDownloadTask, soFarBytes: Int, totalBytes: Int) {
            Log.i(
                "FileDownloader",
                "progress: soFarBytes = $soFarBytes totalBytes = $totalBytes"
            )
            call?.onProgress(soFarBytes, totalBytes.toLong())
        }

        //在完成前同步调用该方法，此时已经下载完成
        override fun blockComplete(task: BaseDownloadTask) {
            Log.i("FileDownloader", "blockComplete")
            //操作完成所以操作，在这里回调
            call?.onSuccess(task.path)
        }

        //重试之前把将要重试是第几次回调回来
        override fun retry(
            task: BaseDownloadTask, ex: Throwable, retryingTimes: Int, soFarBytes: Int
        ) {
            Log.i(
                "FileDownloader",
                "retry: ex = $ex retryingTimes = $retryingTimes soFarBytes = $soFarBytes"
            )
        }

        //完成整个下载过程
        override fun completed(task: BaseDownloadTask) {
            Log.i("FileDownloader", "completed")
            call?.onEnd()
        }

        //暂停下载
        override fun paused(task: BaseDownloadTask, soFarBytes: Int, totalBytes: Int) {
            Log.i("FileDownloader", "paused: soFarBytes = $soFarBytes totalBytes = $totalBytes")
        }

        //下载出现错误
        override fun error(task: BaseDownloadTask, e: Throwable) {
            Log.i("FileDownloader", "error: e = $e")
            e.message?.let { call?.onError(it) }
        }

        //在下载队列中(正在等待/正在下载)已经存在相同下载连接与相同存储路径的任务
        override fun warn(task: BaseDownloadTask) {
            Log.i("FileDownloader", "warn")
        }
    }
}